package com.qs.controller;


import com.qs.model.entity.Address;
import com.qs.response.Response;
import com.qs.service.IAddressService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户地址表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-04-02
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/address")
public class AddressController {

    private final IAddressService addressService;

    /**
     * 新增收货地址
     * @param address
     * @return
     */
    @PostMapping("/save")
    public Response saveAddress(@RequestBody Address address){
        log.info("id为{}的用户正在新增收货地址请求....", address.getUserId());
        addressService.saveAddress(address);
        log.info("id为{}的用户新增收货地址请求处理成功....", address.getUserId());
        return Response.success();
    }

    /**
     * 修改收货地址
     * @param address
     * @return
     */
    @PostMapping("/update")
    public Response updateAddress(@RequestBody Address address){
        log.info("id为{}的用户正在修改id为{}的收货地址请求....", address.getUserId(),address.getId());
        addressService.updateAddress(address);
        log.info("id为{}的用户修改id为{}的地址请求处理成功....", address.getUserId(),address.getId());
        return Response.success();
    }

    /**
     * 获取当前用户的所有收货地址
     * @param userId
     * @return
     */
    @GetMapping("/query/{userId}")
    public Response query(@PathVariable("userId") Integer userId){
        log.info("id为{}的用户正在查询收货地址请求....", userId);
        List<Address> list = addressService.queryAddress(userId);
        log.info("id为{}的用户查询收货地址请求处理成功....", userId);
        return Response.success(list);
    }

    /**
     * 收货地址回显
     * @param id
     * @return
     */
    @GetMapping("/query/echo/{id}")
    public Response echo(@PathVariable("id") Integer id){
        Address address = addressService.getById(id);
        return Response.success(address);
    }
}
